package com.wgq.TestRongQi;

import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;

public class TestProperties {
    private Properties properties = new Properties();
    //创建Properties文件
    private void Create(){
        File file = new File("test.Properties");
        try {
            file.createNewFile();
            properties.load(new FileInputStream("test.Properties"));
//            Enumeration enum1 = properties.propertyNames();//得到配置文件的名字
//                     while(enum1.hasMoreElements()) {
//                            String strKey = (String) enum1.nextElement();
//                            String strValue = properties.getProperty(strKey);
                            // System.out.println(strKey + "=" + strValue);
//                         }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //写数据到文件 写单个属性
    public void writeProperties(String FilePath,String key,String Value){
        try {
            InputStream in  = new FileInputStream(FilePath);
            try {
                properties.load(in);
                properties.setProperty(key,Value);
                OutputStream out = new FileOutputStream(FilePath);
                //输出打印到文件
                properties.store(out,"---"+key);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

   //写多个属性
    private void writeProperties(String FilePath, HashMap map){
        try {
            InputStream in  = new FileInputStream(FilePath);
            try {
                properties.load(in);
                Iterator<HashMap.Entry<Object,String>> it = map.entrySet().iterator();
                while (it.hasNext()) {
                    HashMap.Entry<Object, String> entry = it.next();
                    properties.setProperty((String) entry.getKey(), entry.getValue());
                }
                OutputStream out = new FileOutputStream(FilePath);
                //输出打印到文件
                properties.store(out,"--系统属性--");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    //取系统属性
    private HashMap getSystemProperties(){
        properties = System.getProperties();
        // properties.list(System.out);
        HashMap<String,String> map =new HashMap();
        Enumeration en = properties.propertyNames();
        while (en.hasMoreElements()){
          String key = (String)en.nextElement();
          String value = properties.getProperty(key);
          map.put(key,value);
        }
        return map;
    }
    public static void main(String[] args) {
       TestProperties tp = new TestProperties();
        tp.Create();
        //单个属性
//        tp.writeProperties("test.Properties","name","wgq");
        //多个属性
        tp.writeProperties("test.Properties",tp.getSystemProperties());
//          tp.properties = System.getProperties();
//          tp.properties.list(System.out);


    }

}
